//
//  MJDIYHeader.m
//  MJRefreshExample
//
//  Created by MJ Lee on 15/6/13.
//  Copyright © 2015年 小码哥. All rights reserved.
//

#import "MJDIYHeader.h"

@interface MJDIYHeader()

@property (nonatomic,strong)CADisplayLink *timer;
@property (weak, nonatomic) UILabel *label;
@property (weak, nonatomic) UIImageView *logo;
@property (nonatomic,strong)CALayer * layer1;
@property (nonatomic,strong)CALayer * layermin;
@property (nonatomic,strong)CALayer * layertime;
@property (nonatomic,strong) CABasicAnimation * animation;
@property (nonatomic,strong) UIImageView *zhuan;
@property (nonatomic,strong) CABasicAnimation* rotationAnimation;

@end

@implementation MJDIYHeader
#pragma mark - 重写方法
#pragma mark 在这里做一些初始化配置（比如添加子控件）
- (void)prepare
{
    [super prepare];
    
    self.backgroundColor = [UIColor clearColor];
    
    // 设置控件的高度
    self.mj_h = 70;

    UIImageView * imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"logo"]];
    [self addSubview:imageView];
    
    self.logo = imageView;
    
    self.zhuan = [[UIImageView alloc]init];
    self.zhuan.frame = CGRectMake(0, 0, 55, 55);
    self.zhuan.image = [UIImage imageNamed:@"loading"];
    [self addSubview:self.zhuan];
    
}

#pragma mark 在这里设置子控件的位置和尺寸
- (void)placeSubviews
{
    [super placeSubviews];
    
    self.logo.frame = CGRectMake(0, 0, 50, 50);
    self.logo.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);

    self.zhuan.center  = self.logo.center;
    
    _rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    _rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    _rotationAnimation.duration = 0.4;
    _rotationAnimation.cumulative = YES;
    _rotationAnimation.repeatCount = 10000;
    
}

#pragma mark 监听scrollView的contentOffset改变
- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change
{
    [super scrollViewContentOffsetDidChange:change];
}

#pragma mark 监听scrollView的contentSize改变
- (void)scrollViewContentSizeDidChange:(NSDictionary *)change
{
    [super scrollViewContentSizeDidChange:change];
    
}

#pragma mark 监听scrollView的拖拽状态改变
- (void)scrollViewPanStateDidChange:(NSDictionary *)change
{
    [super scrollViewPanStateDidChange:change];
    
}

#pragma mark 监听控件的刷新状态
- (void)setState:(MJRefreshState)state
{
    MJRefreshCheckState;

    switch (state) {
        case MJRefreshStateIdle:
           
            self.label.text = @"赶紧下拉吖";
            break;
        case MJRefreshStatePulling:
            self.label.text = @"赶紧放开我吧";
            
            break;
        case MJRefreshStateRefreshing:
           
            self.label.text = @"加载数据中";
            [self.zhuan.layer addAnimation:_rotationAnimation forKey:@"rotationAnimation"];
            
            break;
        default:
            break;
    }
}

-(void)endRefreshing{
    [super endRefreshing];
    
    [self.zhuan.layer removeAllAnimations];

}

#pragma mark 监听拖拽比例（控件被拖出来的比例）
- (void)setPullingPercent:(CGFloat)pullingPercent
{
    [super setPullingPercent:pullingPercent];

    if (self.isRefreshing) return;

    self.zhuan.transform=CGAffineTransformMakeRotation(M_PI*(pullingPercent*2));

}

@end
